import sys
import wmi
from PyQt5.Qt import *
from netifaces import interfaces, ifaddresses, AF_INET


class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.resize(800, 550)
        self.setWindowTitle("一键获取电脑配置信息")
        # 添加显示区域
        self.textBrowser = QTextBrowser()
        # 设置显示区域字体大小
        font = QFont()
        font.setFamily("宋体")
        font.setPointSize(14)
        self.textBrowser.setFont(font)
        # 添加Button
        self.button1 = QPushButton()
        self.button1.setText("一键获取电脑配置信息")
        # 设置Button字体大小
        font = QFont()
        font.setFamily("宋体")
        font.setPointSize(16)
        self.button1.setFont(font)
        # 将信号与槽关联
        self.button1.clicked.connect(self.get)
        # 把按钮放在窗口上
        # layout = QHBoxLayout()  # 水平布局
        layout = QVBoxLayout()  # 垂直布局
        layout.addWidget(self.textBrowser)
        layout.addWidget(self.button1)
        # 主框架，所有控件的放置位置
        mainFrame = QWidget()
        mainFrame.setLayout(layout)
        # # 使充满屏幕
        self.setCentralWidget(mainFrame)

    def get(self):
        infoComputer = ''
        c = wmi.WMI()

        # 操作系统版本，版bai本号，32位/64位
        # infoComputer = infoComputer + '---------- 电脑基本信息 ----------' + '\n'

        sys = c.Win32_OperatingSystem()[0]
        infoComputer = infoComputer + sys.Caption + ' ' + sys.OSArchitecture + '\n'

        # 处理器
        processor = c.Win32_Processor()[0]
        infoComputer = infoComputer + '处理器：' + processor.Name.strip() + '\n'

        # 内存
        sum = 0
        for memModule in c.Win32_PhysicalMemory():
            sum += int(memModule.Capacity) / 1024 ** 3
        infoComputer = infoComputer + '内存：(' + str(sum) + 'GB' + ')\n'
        for memModule in c.Win32_PhysicalMemory():
            totalMemSize = int(memModule.Capacity)
            infoComputer = infoComputer + '      内存厂商：' + memModule.Manufacturer + '  ' + '内存型号：' + memModule.PartNumber + \
                           '      内存大小：' + str(totalMemSize / 1024 ** 3) + 'GB' + '\n'

        # 显卡
        infoComputer = infoComputer + '显卡：' + '\n'
        for xk in c.Win32_VideoController():
            infoComputer = infoComputer + '      ' + xk.name + '\n'

            # 主板
        for board_id in c.Win32_BaseBoard():
            infoComputer = infoComputer + '主板：' + board_id.Manufacturer + board_id.Product + '\n'

        # 硬盘
        infoComputer = infoComputer + '硬盘：' + '\n'
        for disk in c.Win32_DiskDrive(InterfaceType="IDE"):
            diskSize = int(disk.size)
            infoComputer = infoComputer + '      名称：' + disk.Caption + '    '
            infoComputer = infoComputer + '大小：' + str(diskSize / 1024 ** 3) + 'GB' + '\n'

        # IP地址
        infoComputer += '\n'
        for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
            infoComputer = infoComputer + 'MAC地址：' + interface.MACAddress + '\n'

        for ip_address in interface.IPAddress:
            for i in ip_address:
                if i == '.':
                    infoComputer = infoComputer + 'IPv4地址：' + ip_address + '\n'
                    break
                if i == ':':
                    infoComputer = infoComputer + 'IPv6地址：' + ip_address + '\n'
                    break

        self.textBrowser.setText(infoComputer)

        # IPv4地址
        infoComputer = infoComputer + '\n' + "远程桌面连接用IPv4地址" + '\n'
        strRes = ''
        for ifaceName in interfaces():
            addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr': 'No IP addr'}])]
            ipAdd = ' '.join(addresses)
            if ipAdd != 'No IP addr':
                if strRes == '':
                    strRes = ipAdd
                else:
                    strRes += '\n' + ipAdd
        if strRes == '':
            infoComputer += "查找不到IP地址，请检查网络连接"
        else:
            infoComputer += strRes

        self.textBrowser.setText(infoComputer)


if __name__ == '__main__':
    App = QApplication(sys.argv)
    Win = MyWindow()
    Win.show()
    sys.exit(App.exec_())
